package org.jeecgframework.web.util;

import java.io.File;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.util.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public class FileUtil {
	
	public static FileBean uploadFile(HttpServletRequest request){
		FileBean fileBean = new FileBean();
		try {
			MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
			MultipartFile multipartFile = req.getFile("Filedata");
			//文件名称
			String fileRealName=multipartFile.getOriginalFilename();
			//文件后缀名
			String fileSuffix = FileUtils.getExtend(fileRealName);
			//System.out.println(fileSuffix);
			String fileName = UUID.randomUUID().toString().replace("-", "") + "." + fileSuffix;
			//System.out.println("UUID.randomUUID().toString():"+UUID.randomUUID().toString());
			//System.out.println(fileName);
			//文件全路径
			String uploadPath = FilmStaticValue.File_Path + fileName;
			//System.out.println(uploadPath);
			isMkdir(FilmStaticValue.File_Path);
			File file = new File(uploadPath);
			multipartFile.transferTo(file);
			fileBean.setFileName(fileName);
			fileBean.setFilePath(uploadPath);
			fileBean.setFileRealName(fileRealName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return fileBean;
	}
	
	/**
	 * 判断文件夹是否存在   如果不存在则创建
	 * @param path 文件夹地址
	 */
	public static void isMkdir(String path){
		File folder = new File(path);
		
		if(!folder .exists()  && !folder .isDirectory()){
			folder.mkdir();
		}
	}

}
